﻿using IceCS.Permission.Core.Enums;

namespace IceCS.Permission.Core
{
    public class UserActionManager
    {
        /// <summary>
        /// 检查是否允许某种行为
        /// </summary>
        /// <param name="userAction">用户现允许行为</param>
        /// <param name="action">判断包含行为</param>
        /// <returns></returns>
        public static bool CheckUserHasOwnAction(UserAction userAction, UserAction action)
        {
            return userAction.HasFlag(action);
            //return (userAction & action) != 0;
        }
        /// <summary>
        /// 添加用户行为
        /// </summary>
        /// <param name="action">用户现有行为</param>
        /// <param name="newAction">添加的行为</param>
        /// <returns></returns>
        public static UserAction AddUserAction(UserAction userAction, UserAction newAction)
        {
            return userAction | newAction;
        }
        /// <summary>
        /// 移除用户行为
        /// </summary>
        /// <param name="action">用户现有行为</param>
        /// <param name="removeAction">移除的行为</param>
        /// <returns></returns>
        public static UserAction RemoveUserAction(UserAction userAction, UserAction removeAction)
        {
            return userAction & ~removeAction;
        }
        /// <summary>
        /// 反转一个用户的行为，有就删，无就加
        /// </summary>
        /// <param name="userAction"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static UserAction ReverseUserAction(UserAction userAction, UserAction action)
        {
            return userAction ^ action;
            /*
            UserAction returnAction;
            if (CheckUserHasOwnAction(userAction, action))
            {
                returnAction = RemoveUserAction(userAction, action);
            }
            else
            {
                returnAction = AddUserAction(userAction, action);
            }
            return returnAction;
            */
        }
    }
}
